
clear all 

//define folder global here

gl Inputs "$folder\1_Inputs"
gl Intermediate "$folder\2_Intermediate"
gl Outputs "$folder\3_Outputs"

********************************************************************************
********************************************************************************
**# TABLE G.1

import delimited "${Inputs}\Cook_stringency_specification_checks.tex", clear 

drop if regexm(v1, "tabular|hline")
replace v1 = v1 + "," + v2 if !mi(v2)
drop v2 

replace v1 = subinstr(v1, "$", "", .)
replace v1 = subinstr(v1, "\\", "", .)
split v1, parse("&")
drop v1 

rename (v11 v12 v13 v14 v15) (sample var_coef var_se var_pval var_obs)
drop if sample=="Sample "
drop if regexm(sample, "%") // these are not being shown in the overleaf anyway

gen geo = "cc"

replace sample = "main" if regexm(sample, "Main")
replace sample = "jchar" if regexm(sample, "Controlling for other judge ch")
replace sample = "altj" if regexm(sample, "Alternate first judge constr")
replace sample = "allc" if regexm(sample, "All cases")
replace sample = "nser" if regexm(sample, "Excluding cases never served")

reshape long var_, i(sample) j(item) string 

tempfile cooks
save `cooks'
 

* IMPORT NEW NY DATA TO APPEND

use "${Inputs}\NY_fs_robustness.dta", clear

gen var_pval = string(2 * (1-normal(abs(beta/se))), "%10.3fc")
gen var_coef = string(beta, "%10.3fc")
gen var_se = string(se, "%10.3fc")
gen var_obs = string(N, "%10.0fc")

gen 	sample = "main" if regexm(spec, "Main")
replace sample = "altc" if regexm(spec, "Alternate Courtroom Construction")
replace sample = "othst" if regexm(spec, "Controlling for other stringencies")
replace sample = "allc" if regexm(spec, "All cases")

drop beta se N specification

gen geo = "ny"

reshape long var_, i(sample) j(item) string

append using `cooks'
compress 

isid geo sample item
order geo sample item, first 

levelsof geo, local(geos)
levelsof sample, local(samps)
levelsof item, local(ites)

foreach gex in `geos' {
	local id1 = "`gex'"
	
	foreach sax in `samps' {
		local id2 = "`sax'"
		
		foreach itex in `ites' {
			local id3 = "`itex'"
			
			levelsof var_ if geo=="`gex'" & sample=="`sax'" & item=="`itex'", ///
			clean local(`id1'_`id2'_`id3')
			
			di "`id1'_`id2'_`id3'"
		}
	}
}
	
 
texdoc init "${Outputs}/Supplementary data_Table_G1.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c c c} \hline \toprule ///
	Sample & Coefficient & Standard Error & P-Value & Observations \\
tex \midrule 

tex \multicolumn{5}{l}{\textit{Cook County}} \\
tex ~~Main & `cc_main_coef' & `cc_main_se' & `cc_main_pval' & `cc_main_obs' \\
tex ~~Controlling for other judge stringency dimensions & `cc_jchar_coef' & `cc_jchar_se' & `cc_jchar_pval' & `cc_jchar_obs' \\
tex ~~Alternate first judge construction & `cc_altj_coef' & `cc_altj_se' & `cc_altj_pval' & `cc_altj_obs' \\
tex ~~All cases & `cc_allc_coef' & `cc_allc_se' & `cc_allc_pval' & `cc_allc_obs' \\
tex ~~Excluding cases never served & `cc_nser_coef' & `cc_nser_se' & `cc_nser_pval' & `cc_nser_obs' \\

tex \midrule
tex \multicolumn{5}{l}{\textit{New York}} \\
tex ~~Main & `ny_main_coef' & `ny_main_se' & `ny_main_pval' & `ny_main_obs' \\
tex ~~Alternate courtroom construction & `ny_altc_coef' & `ny_altc_se' & `ny_altc_pval' & `ny_altc_obs' \\
tex ~~Controlling for other stringencies & `ny_othst_coef' & `ny_othst_se' & `ny_othst_pval' & `ny_othst_obs' \\
tex ~~All cases & `ny_allc_coef' & `ny_allc_se' & `ny_allc_pval' & `ny_allc_obs' \\


tex \bottomrule \\
tex	\end{tabular} }
texdoc close



********************************************************************************
********************************************************************************
**# TABLE G.2

use "${Inputs}\NY_fs_subgroups.dta", clear
	keep if inlist(group, "FEMALE", "BLACK", "ALL")
	keep if regexm(specification, "With Controls")

	gen fstat_ny = string(fstat, "%10.0fc")
	keep fstat_ny group
	replace group = lower(group)
	replace group = "fem" if group=="female"
	replace group = "bla" if group=="black"
	
	tempfile ny_fstats
	save `ny_fstats'


import delimited "${Inputs}\Cook_lehd_fs.csv", clear
	keep if inlist(population, "all=1", "fem=1", "black=1")
	keep if specification=="(2)"
	
	gen fstat_cc = string(f_inst, "%10.0fc")
	keep population fstat_cc
	
	rename population group
	replace group = subinstr(group, "=1", "", .)
	replace group = "bla" if group=="black"

	merge 1:1 group using `ny_fstats', assert(3) nogen
	compress
	
	
levelsof group, local(groupz)	
foreach grz in `groupz' {
	local id = "`grz'"
	
	levelsof fstat_cc if group=="`grz'", clean local(cc_`id')
	levelsof fstat_ny if group=="`grz'", clean local(ny_`id')
}	
	
	
texdoc init "${Outputs}/Supplementary data_Table_G2.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c} \hline \toprule  ///
& Cook County & New York \\
tex  & (1) & (2) \\
tex \cline{2-3}
tex & & \\
tex F-Statistic - Full Sample & `cc_all' & `ny_all' \\
tex & & \\
tex F-Statistic - Female Sample & `cc_fem' & `ny_fem' \\
tex & & \\
tex F-Statistic - Black Sample & `cc_bla' & `ny_bla' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close




********************************************************************************
********************************************************************************
**# TABLE G.3

import delimited "${Inputs}\Cook_experian_fs.csv", clear
rename (spec beta_est beta_se n_obs_mean beta_name) (specification beta se N coef_var)
gen geo = "cc"

append using "${Inputs}\NY_credit_fs.dta"
replace geo = "ny" if mi(geo)

assert coef_var=="Stringency instrument" | coef_var=="Coefficent on Instrument"
drop coef_var 

gen pval = 2*(1-normal(abs(beta / se)))
gen 	stars = "***" 	if pval < 0.01
replace stars = "**" 	if pval < 0.05 	& mi(stars)
replace stars = "*" 	if pval < 0.1 	& mi(stars)	
		

gen num_co = string(beta, "%10.3fc") + stars
gen num_se = "(" + string(se, "%10.3fc") + ")"
gen num_obs = string(N, "%10.0fc")

gen 	spec = "w_cont" if specification=="First stage with controls" | specification=="First Stage, With Controls"
replace spec = "no_cont" if specification=="First stage without controls" | specification=="First Stage, No Controls"

drop specification beta se N

reshape long num , i(spec geo) j(value) string 
replace value = subinstr(value, "_", "", .)
order geo spec value


levelsof geo, local(geos)
levelsof spec, local(specs)
levelsof value, local(valus)

foreach geox in `geos' {
	local id1 = "`geox'"
	
	foreach spex in `specs' {
		local id2 = "`spex'"
		
		foreach valx in `valus' {
			local id3 = "`valx'"
		
		levelsof num if geo=="`geox'" & spec=="`spex'" & value=="`valx'", ///
		clean local(`id1'_`id2'_`id3') 
		
		di "`id1'_`id2'_`id3'"		
	}
}
}
				
// to start off lines without errors
gl x " "

texdoc init "${Outputs}/Supplementary data_Table_G3.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c c c c} \hline \toprule  ///
& \multicolumn{2}{c}{\textbf{Cook County}} & & \multicolumn{2}{c}{\textbf{New York}} \\
tex  & (1) & (2) & & (3) & (4) \\
tex \cline{2-3} \cline{5-6}
tex & & & & & \\
tex $x Judge stringency 		& `cc_no_cont_co' & `cc_w_cont_co' 	& & `ny_no_cont_co' & `ny_w_cont_co' \\
tex 					 		& `cc_no_cont_se' & `cc_w_cont_se' 	& & `ny_no_cont_se' & `ny_w_cont_se' \\
tex $x Controls 				& No & Yes & & No & Yes \\
tex \midrule
tex $x Observations 			& `cc_no_cont_obs' & `cc_w_cont_obs' & & `ny_no_cont_obs' & `ny_w_cont_obs' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close



